import { LGXEnums } from "../../../../canvas/common/sysenum";
import fillinfo from "../../../../canvas/common/struct/fillinfo";
import lgxcom from "../../../../canvas/common/common";

var AttachDefs2GraphSvc = {}
//图形对象属性在defs预定义的，通过此函数赋值给图形对象
AttachDefs2GraphSvc.refSvgInfo2Defs = function (graphArr, linegradientArr) {
    for (let i = 0; i < graphArr.length; i++) {
        let graphInfo = graphArr[i];
        let fillMode = graphInfo.fillMode;
        if (fillMode && fillMode != LGXEnums.LGXFillStyle.SOLID && fillMode.indexOf('url') >= 0) {
            let refID = fillMode.replace('url', '');
            refID = refID.replace('#', '');
            refID = refID.replace('(', '');
            refID = refID.replace(')', '');
            let destInfo = this.getDestLineGradient(refID, linegradientArr);
            if (destInfo) {
                this.assignGraphLineGradientProp(graphInfo, destInfo);
            }
        }
    }
}

AttachDefs2GraphSvc.assignGraphLineGradientProp = function (graphInfo, destInfo) {
    let fillInfo = graphInfo.fillInfo;
    graphInfo.fillMode = LGXEnums.LGXFillStyle.lineGradient_complex;
    fillInfo.gradientInfo = new fillinfo.GradientInfo();
    fillInfo.gradientInfo.setStopPos('st',{x:destInfo.x1,y:destInfo.y1});
    fillInfo.gradientInfo.setStopPos('end',{x:destInfo.x2,y:destInfo.y2});
    fillInfo.gradientInfo.stopColorArr = [];
    for (let i = 0; i < destInfo.stopArr.length; i++) {
        let t = destInfo.stopArr[i];
        let stop = t.offset;
        let colorVal = t.color;
        let colorObj = new lgxcom.LGXColor();
        colorObj.setHtmlColor(colorVal);
        fillInfo.gradientInfo.stopColorArr.push({ stop, color: colorObj });
    }
}

AttachDefs2GraphSvc.getDestLineGradient = function (id, linegradientArr) {
    let destInfo = null;
    linegradientArr.forEach(t => {
        if (t.id == id) {
            destInfo = t;
            return;
        }
    });
    return destInfo;
}

export { AttachDefs2GraphSvc }